// addrspace.h 
//	Data structures to keep track of executing user programs 
//	(address spaces).
//
//	For now, we don't keep any information about address spaces.
//	The user level CPU state is saved and restored in the thread
//	executing the user program (see thread.h).
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved.  See copyright.h for copyright notice and limitation 
// of liability and disclaimer of warranty provisions.

#ifndef ADDRSPACE_H
#define ADDRSPACE_H

#include "copyright.h"
#include "filesys.h"
#include "table.h"
#include "noff.h"
#define UserStackSize		1024 	// increase this as necessary!

#define MaxOpenFiles 256
#define MaxChildSpaces 256

//#ifndef BITMAP
//#define BITMAP
//BitMap *mainMemory = new BitMap(512);//512 for 512 pages
//#endif

#define MAX_THREADS 5 //max threads per process

enum Location{InSwapFile, InExecutable, InMainMemory, None};

struct PageTableInfo
{
  Location location;
  int swapFilePosition;
};



class AddrSpace {
  public:
    AddrSpace(OpenFile *executable);	// Create an address space,
					// initializing it with the program
					// stored in the file "executable"
    ~AddrSpace();			// De-allocate an address space

    void InitRegisters();		// Initialize user-level CPU registers,
					// before jumping to user code

    void SaveState();			// Save/restore address space-specific
    void RestoreState();		// info on a context switch
    Table fileTable;			// Table of openfiles

    void Clear(int i);                  //clear a pageTable entry
    int Find(int physicalPage);         //find an entry and set it
    void freeMainMemory();              //when removing a process,freeMM
	 void freeSwap();  //TEMP

    int NewThreadStack(int *bitmapLoc);  //Make a stack for the new thread
    
    bool validateVaddr(int vaddr);            //validate a virtual address
    
   
	 PageTableInfo *pageTableInfo; //Additional information for each page of the page table
	 TranslationEntry *pageTable;	// Assume linear page table translation
	 NoffHeader noffH;
	 
	 OpenFile *execFile;
	 
	 int numCodePages;             //used to check if we are writing code pages
	 
 private:
	 
	 // for now!
    unsigned int numPages;		// Number of pages in the virtual 
	 // address space
    Lock *addrSpaceLock;
    
	 

};

#endif // ADDRSPACE_H
